package com.lenovo.smarttraffic.ui.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.GongJiaoBean_7;
import com.lenovo.smarttraffic.util.DataUtil_7;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Activity_7_2 extends AppCompatActivity {
    private ImageView icon;
    private TextView title;
    private TextView a_7_2_tv1;
    private TextView a_7_2_tv2;
    private TextView a_7_2_tv22;
    private TextView a_7_2_tv3;
    private TextView a_7_2_tv4;
    private ListView a_7_2_lv1;
    private int POSITION = 1;
    RequestQueue queue;
    List<GongJiaoBean_7.ROWSDETAILBean> beans = new ArrayList<>();
    int weizhi = -1;
    MyAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_7_2);

        POSITION = Integer.parseInt(getIntent().getStringExtra("position"));

        initView();

        getdata();

    }

    private void getdata() {
        JSONObject object = new JSONObject();
        try {
            object.put("Line",0);
            object.put("UserName","user1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(JsonRequest.Method.POST, "http://192.168.1.107:8088/transportservice/action/GetBusInfo.do", object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                beans = new Gson().fromJson(jsonObject.toString(), GongJiaoBean_7.class).getROWS_DETAIL();
                a_7_2_tv1.setText(beans.get(POSITION).getTime().get(0).getSite()+"-"+beans.get(POSITION).getTime().get(1).getSite());
                a_7_2_tv2.setText(beans.get(POSITION).getTime().get(0).getStarttime());
                a_7_2_tv22.setText(beans.get(POSITION).getTime().get(1).getEndtime());
                a_7_2_tv3.setText(beans.get(POSITION).getSites().size()+"站/"+beans.get(POSITION).getMileage()+"公里");
                a_7_2_tv4.setText("票价：最高票价"+beans.get(POSITION).getTicket()+"元");
                DataUtil_7.strings.add(POSITION+"路（"+beans.get(POSITION).getTime().get(0).getSite()+"-"+beans.get(POSITION).getTime().get(1).getSite()+")");
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        queue.add(request);
    }

    private void initView() {
        icon = (ImageView) findViewById(R.id.icon);
        title = (TextView) findViewById(R.id.title);
        a_7_2_tv1 = (TextView) findViewById(R.id.a_7_2_tv1);
        a_7_2_tv2 = (TextView) findViewById(R.id.a_7_2_tv2);
        a_7_2_tv22 = (TextView) findViewById(R.id.a_7_2_tv22);
        a_7_2_tv3 = (TextView) findViewById(R.id.a_7_2_tv3);
        a_7_2_tv4 = (TextView) findViewById(R.id.a_7_2_tv4);
        a_7_2_lv1 = (ListView) findViewById(R.id.a_7_2_lv1);
        queue = Volley.newRequestQueue(Activity_7_2.this);
        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        title.setText(POSITION + "路");

        adapter = new MyAdapter();

        a_7_2_lv1.setAdapter(adapter);

    }

    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return beans.size();
        }

        @Override
        public Object getItem(int i) {
            return beans.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @SuppressLint("NewApi")
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            view = getLayoutInflater().inflate(R.layout.a_7_2_lv, null);

            ViewHolder holder = new ViewHolder(view);

            int a=i+1;

            if(i==weizhi){
                holder.a_7_2_lv1_tv1.setBackground(getDrawable(R.drawable.hongyuan));
                holder.a_7_2_lv1_tv1.setTextColor(Color.WHITE);
                holder.a_7_2_lv1_tv2.setTextColor(Color.RED);
            }else {
                holder.a_7_2_lv1_tv1.setBackground(getDrawable(R.drawable.lanyuan));
                holder.a_7_2_lv1_tv1.setTextColor(Color.parseColor("#6748c4"));
                holder.a_7_2_lv1_tv2.setTextColor(Color.BLACK);
            }


            holder.a_7_2_lv1_tv1.setText(a+"");
            holder.a_7_2_lv1_tv2.setText(beans.get(POSITION).getSites().get(i));

            holder.a_7_2_lv1_tv2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    weizhi = i;
                    notifyDataSetChanged();
                }
            });

            return view;
        }

        class ViewHolder {
            public View rootView;
            public TextView a_7_2_lv1_tv1;
            public TextView a_7_2_lv1_tv2;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.a_7_2_lv1_tv1 = (TextView) rootView.findViewById(R.id.a_7_2_lv1_tv1);
                this.a_7_2_lv1_tv2 = (TextView) rootView.findViewById(R.id.a_7_2_lv1_tv2);
            }

        }
    }

}
